package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author mqx
 * @date 2020-10-17 14:49:16
 */
@Service
public class UserServiceImpl implements UserService {

    // 调用mapper
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public UserInfo login(UserInfo userInfo) {
        //  select * from user_info where userName = ? and pwd = ?
        //  数据库密码需要加密：
        String passwd = userInfo.getPasswd();
        //  MD5
        String newPwd = DigestUtils.md5DigestAsHex(passwd.getBytes());
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("login_name",userInfo.getLoginName());
        userInfoQueryWrapper.eq("passwd",newPwd);
        // 查询之后获取到的对象
        UserInfo info = userInfoMapper.selectOne(userInfoQueryWrapper);
        if (info!=null){
            return info;
        }
        return null;
    }
}
